package com.unity3d.player;

import java.util.*;
import java.net.*;
import javax.net.ssl.*;
import java.io.*;

class WWW extends Thread
{
    private int a;
    private int b;
    private String c;
    private byte[] d;
    private Map e;

    WWW(final int b, final String c, final byte[] d, final Map e) {
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
        this.a = 0;
    }

    private static native void doneCallback(final int p0);

    private static native void errorCallback(final int p0, final String p1);

    private static native boolean headerCallback(final int p0, final String p1);

    private static native void progressCallback(final int p0, final float p1, final float p2, final double p3, final int p4);

    private static native boolean readCallback(final int p0, final byte[] p1, final int p2);

    protected boolean headerCallback(final String s, final String s2) {
        final StringBuilder sb = new StringBuilder();
        sb.append(s);
        sb.append(": ");
        sb.append(s2);
        sb.append("\n\r");
        return headerCallback(this.b, sb.toString());
    }

    protected boolean headerCallback(final Map map) {
        if (map != null && map.size() != 0) {
            final StringBuilder sb = new StringBuilder();
            for (final Map.Entry<K, List> entry : map.entrySet()) {
                for (final String s : entry.getValue()) {
                    sb.append((String)entry.getKey());
                    sb.append(": ");
                    sb.append(s);
                    sb.append("\r\n");
                }
                if (entry.getKey() == null) {
                    for (final String s2 : entry.getValue()) {
                        sb.append("Status: ");
                        sb.append(s2);
                        sb.append("\r\n");
                    }
                }
            }
            return headerCallback(this.b, sb.toString());
        }
        return false;
    }

    protected void progressCallback(int max, final int n, final int n2, final int n3, final long n4, final long n5) {
        final double n6 = 0.0;
        double n12;
        float n13;
        float n14;
        if (n3 > 0) {
            final float n7 = n2 / (float)n3;
            max = Math.max(n3 - n2, 0);
            final double n8 = n2;
            Double.isNaN(n8);
            final double n9 = n8 * 1000.0 / Math.max((double)(n4 - n5), 0.1);
            final double n10 = max;
            Double.isNaN(n10);
            final double n11 = n10 / n9;
            n12 = n6;
            if (!Double.isInfinite(n11)) {
                if (Double.isNaN(n11)) {
                    n12 = n6;
                }
                else {
                    n12 = n11;
                }
            }
            n13 = 1.0f;
            n14 = n7;
        }
        else {
            if (n <= 0) {
                return;
            }
            final float n15 = (float)(max / n);
            n12 = 0.0;
            n14 = 0.0f;
            n13 = n15;
        }
        progressCallback(this.b, n13, n14, n12, n3);
    }

    protected boolean readCallback(final byte[] array, final int n) {
        return readCallback(this.b, array, n);
    }

    @Override
    public void run() {
        try {
            this.runSafe();
        }
        catch (Throwable t) {
            final int b = this.b;
            final StringBuilder sb = new StringBuilder("Error: ");
            sb.append(t.toString());
            errorCallback(b, sb.toString());
        }
    }

    public void runSafe() {
        final int a = this.a;
        final int n = 1;
        final int a2 = a + 1;
        this.a = a2;
        if (a2 > 5) {
            errorCallback(this.b, "Too many redirects");
            return;
        }
        Label_0830_Outer:
        while (true) {
            Label_0914:
            while (true) {
                Label_0830:
                while (true) {
                    Label_1012: {
                        Label_1010:
                        while (true) {
                            Label_1002: {
                                try {
                                    final URL url = new URL(this.c);
                                    final URLConnection openConnection = url.openConnection();
                                    if (openConnection instanceof HttpsURLConnection) {
                                        final SSLSocketFactory a3 = com.unity3d.player.a.a();
                                        if (a3 != null) {
                                            ((HttpsURLConnection)openConnection).setSSLSocketFactory(a3);
                                        }
                                    }
                                    if (url.getProtocol().equalsIgnoreCase("file") && url.getHost() != null && url.getHost().length() != 0) {
                                        final int b = this.b;
                                        final StringBuilder sb = new StringBuilder();
                                        sb.append(url.getHost());
                                        sb.append(url.getFile());
                                        sb.append(" is not an absolute path!");
                                        errorCallback(b, sb.toString());
                                        return;
                                    }
                                    final Map e = this.e;
                                    if (e != null) {
                                        for (final Map.Entry<String, V> entry : e.entrySet()) {
                                            openConnection.addRequestProperty(entry.getKey(), (String)entry.getValue());
                                        }
                                    }
                                    final byte[] d = this.d;
                                    int min = 1428;
                                    int read = 0;
                                    if (d != null) {
                                        openConnection.setDoOutput(true);
                                        try {
                                            final OutputStream outputStream = openConnection.getOutputStream();
                                            int i = 0;
                                            while (i < this.d.length) {
                                                final int min2 = Math.min(1428, this.d.length - i);
                                                outputStream.write(this.d, i, min2);
                                                i += min2;
                                                this.progressCallback(i, this.d.length, 0, 0, 0L, 0L);
                                            }
                                        }
                                        catch (Exception ex) {
                                            errorCallback(this.b, ex.toString());
                                            return;
                                        }
                                    }
                                    if (openConnection instanceof HttpURLConnection) {
                                        final HttpsURLConnection httpsURLConnection = (HttpsURLConnection)openConnection;
                                        try {
                                            final int responseCode = httpsURLConnection.getResponseCode();
                                            final Map<String, List<String>> headerFields = httpsURLConnection.getHeaderFields();
                                            if (headerFields != null && (responseCode == 301 || responseCode == 302)) {
                                                final List<String> list = headerFields.get("Location");
                                                if (list != null && !list.isEmpty()) {
                                                    httpsURLConnection.disconnect();
                                                    this.c = list.get(0);
                                                    this.run();
                                                    return;
                                                }
                                            }
                                        }
                                        catch (IOException ex2) {
                                            errorCallback(this.b, ex2.toString());
                                            return;
                                        }
                                    }
                                    final Map<String, List<String>> headerFields2 = openConnection.getHeaderFields();
                                    final boolean headerCallback = this.headerCallback(headerFields2);
                                    boolean b2 = false;
                                    Label_0545: {
                                        if (headerFields2 != null) {
                                            b2 = headerCallback;
                                            if (headerFields2.containsKey("content-length")) {
                                                break Label_0545;
                                            }
                                        }
                                        b2 = headerCallback;
                                        if (openConnection.getContentLength() != -1) {
                                            b2 = (headerCallback || this.headerCallback("content-length", String.valueOf(openConnection.getContentLength())));
                                        }
                                    }
                                    boolean b3 = false;
                                    Label_0611: {
                                        if (headerFields2 != null) {
                                            b3 = b2;
                                            if (headerFields2.containsKey("content-type")) {
                                                break Label_0611;
                                            }
                                        }
                                        b3 = b2;
                                        if (openConnection.getContentType() != null) {
                                            b3 = (b2 || this.headerCallback("content-type", openConnection.getContentType()));
                                        }
                                    }
                                    final String s = " aborted";
                                    if (b3) {
                                        final int b4 = this.b;
                                        final StringBuilder sb2 = new StringBuilder();
                                        sb2.append(this.c);
                                        sb2.append(" aborted");
                                        errorCallback(b4, sb2.toString());
                                        return;
                                    }
                                    int contentLength;
                                    if (openConnection.getContentLength() > 0) {
                                        contentLength = openConnection.getContentLength();
                                    }
                                    else {
                                        contentLength = 0;
                                    }
                                    if (url.getProtocol().equalsIgnoreCase("file") || url.getProtocol().equalsIgnoreCase("jar")) {
                                        if (contentLength == 0) {
                                            min = 32768;
                                        }
                                        else {
                                            min = Math.min(contentLength, 32768);
                                        }
                                    }
                                    try {
                                        final long currentTimeMillis = System.currentTimeMillis();
                                        final byte[] array = new byte[min];
                                        InputStream inputStream = null;
                                        if (!(openConnection instanceof HttpURLConnection)) {
                                            break Label_1002;
                                        }
                                        final HttpsURLConnection httpsURLConnection2 = (HttpsURLConnection)openConnection;
                                        inputStream = httpsURLConnection2.getErrorStream();
                                        final StringBuilder sb3 = new StringBuilder();
                                        sb3.append(httpsURLConnection2.getResponseCode());
                                        sb3.append(": ");
                                        sb3.append(httpsURLConnection2.getResponseMessage());
                                        final String string = sb3.toString();
                                        if (inputStream == null) {
                                            inputStream = openConnection.getInputStream();
                                            final int n2 = 0;
                                            break Label_1012;
                                        }
                                        break Label_1010;
                                        // iftrue(Label_0926:, read == -1)
                                        // iftrue(Label_0939:, n2 == 0)
                                        // iftrue(Label_1017:, n2 != 0)
                                        Block_38: {
                                            break Block_38;
                                            read = inputStream.read(array);
                                            continue Label_0830;
                                            final int b5 = this.b;
                                            final StringBuilder sb4 = new StringBuilder();
                                            sb4.append(this.c);
                                            sb4.append(s);
                                            errorCallback(b5, sb4.toString());
                                            return;
                                            final int n3;
                                            Block_40: {
                                                while (true) {
                                                    Block_41: {
                                                        break Block_41;
                                                        Label_0889: {
                                                            break Block_40;
                                                        }
                                                        this.progressCallback(0, 0, n3, n3, 0L, 0L);
                                                        doneCallback(this.b);
                                                        return;
                                                    }
                                                    errorCallback(this.b, string);
                                                    continue;
                                                }
                                            }
                                            n3 += read;
                                            this.progressCallback(0, 0, n3, contentLength, System.currentTimeMillis(), currentTimeMillis);
                                            continue Label_0914;
                                        }
                                    }
                                    // iftrue(Label_0889:, !this.readCallback(array, read))
                                    catch (Exception ex3) {
                                        errorCallback(this.b, ex3.toString());
                                        return;
                                    }
                                }
                                catch (IOException ex4) {
                                    errorCallback(this.b, ex4.toString());
                                    return;
                                }
                                catch (MalformedURLException ex5) {
                                    errorCallback(this.b, ex5.toString());
                                    return;
                                }
                            }
                            final String string = "";
                            continue Label_0830_Outer;
                        }
                        final int n2 = n;
                    }
                    int n3 = 0;
                    continue Label_0830;
                }
                Label_1017: {
                    continue Label_0914;
                }
            }
        }
    }
}
